package zhongbo.java.air.dictionary.web.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import zhongbo.java.air.dictionary.model.AirAlertZone;
import zhongbo.java.air.dictionary.service.IAirAlertZoneService;
import zhongbo.java.air.dictionary.web.dto.AirAlertZoneDto;
import zhongbo.java.air.dictionary.web.vo.ApiResult;

import java.time.LocalDateTime;
import java.util.List;

@Api(tags = "警戒区域信息管理")
@RestController
@RequestMapping("/api/alert-zone")
public class AirAlertZoneController {

    @Autowired
    private IAirAlertZoneService airAlertZoneService;

    @ApiOperation("创建警戒区域信息")
    @PostMapping
    public ApiResult<AirAlertZone> create(@Validated @RequestBody AirAlertZoneDto dto) {
        AirAlertZone airAlertZone = new AirAlertZone();
        BeanUtils.copyProperties(dto, airAlertZone);
        airAlertZoneService.save(airAlertZone);
        return ApiResult.success(airAlertZone);
    }

    @ApiOperation("更新警戒区域信息")
    @PutMapping
    public ApiResult<AirAlertZone> update(@Validated @RequestBody AirAlertZoneDto dto) {
        if (dto.getId() == null) {
            return ApiResult.validateFailed("更新时ID不能为空");
        }
        AirAlertZone airAlertZone = new AirAlertZone();
        BeanUtils.copyProperties(dto, airAlertZone);
        airAlertZoneService.updateById(airAlertZone);
        return ApiResult.success(airAlertZone);
    }

    @ApiOperation("删除警戒区域信息")
    @DeleteMapping("/{id}")
    public ApiResult<Void> delete(@PathVariable String id) {
        airAlertZoneService.removeById(id);
        return ApiResult.success();
    }

    @ApiOperation("获取警戒区域信息")
    @GetMapping("/{id}")
    public ApiResult<AirAlertZone> getById(@PathVariable String id) {
        return ApiResult.success(airAlertZoneService.getById(id));
    }

    @ApiOperation("根据区域编号获取警戒区域信息")
    @GetMapping("/code/{zoneCode}")
    public ApiResult<AirAlertZone> getByZoneCode(@PathVariable String zoneCode) {
        return ApiResult.success(airAlertZoneService.getByZoneCode(zoneCode));
    }

    @ApiOperation("分页查询警戒区域信息")
    @GetMapping("/page")
    public ApiResult<IPage<AirAlertZone>> page(
            @ApiParam("页码") @RequestParam(defaultValue = "1") long current,
            @ApiParam("每页数量") @RequestParam(defaultValue = "10") long size,
            @ApiParam("排序字段") @RequestParam(required = false) String orderField,
            @ApiParam("是否升序") @RequestParam(defaultValue = "false") boolean isAsc) {
        IPage<AirAlertZone> page = new Page<>(current, size);
        return ApiResult.success(airAlertZoneService.pageWithOrder(page, orderField, isAsc));
    }

    @ApiOperation("根据区域类型查询警戒区域列表")
    @GetMapping("/type/{zoneType}")
    public ApiResult<List<AirAlertZone>> listByZoneType(@PathVariable String zoneType) {
        return ApiResult.success(airAlertZoneService.listByZoneType(zoneType));
    }

    @ApiOperation("查询指定时间有效的警戒区域列表")
    @GetMapping("/effective")
    public ApiResult<List<AirAlertZone>> listEffectiveAt(
            @ApiParam("指定时间") @RequestParam(required = false) 
            @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime dateTime) {
        if (dateTime == null) {
            dateTime = LocalDateTime.now();
        }
        return ApiResult.success(airAlertZoneService.listEffectiveAt(dateTime));
    }
} 